//
//  NodeSetViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2018/12/28.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "NodeSetViewController.h"
#import "MineServer.h"
@interface NodeSetViewController ()
@property (nonatomic,strong) NSMutableArray*dataArr;
@property (nonatomic,strong) MineServer*mineServer;

@end

@implementation NodeSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self getData];
}
- (void)getData{
    [MBProgressHUD showLoadTitile:@"" ToView:self.view];
    [self.mineServer getNodeListComplete:^(BOOL success, NSString * _Nonnull msg) {
        [MBProgressHUD hideHUDForView:self.view];
        if (success) {
            [self.tableView reloadData];
        }else{
            [self.view makeToast:msg];
        }
    }];
}
- (void)setupUI{
    self.tableView.tableFooterView = [UIView new];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mineServer.nodeArr.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.textColor = UIColorHex(333333);
        cell.textLabel.font = kDefaultFont(15);
        UIImageView*choseV = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"wallet_select")];
        choseV.tag = 10001;
        [cell.contentView addSubview:choseV];
        [choseV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(AdaptedWidth(-21));
            make.centerY.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(16, 16));
        }];
    }
    UIImageView*choseV = [cell.contentView viewWithTag:10001];
    
    cell.textLabel.text = self.mineServer.nodeArr[indexPath.row].title;
    choseV.hidden = ![URL_EOS_Main isEqualToString:self.mineServer.nodeArr[indexPath.row].url];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SAVELOCAL(self.mineServer.nodeArr[indexPath.row].url, kMainEOSnode);
    [self.tableView reloadData];
}
#pragma mark - getter
- (MineServer*)mineServer{
    if (!_mineServer) {
        _mineServer = [MineServer new];
    }
    return _mineServer;
}
@end
